package com.baizhi.service.impl;

import com.baizhi.dao.PhoneDAO;
import com.baizhi.entity.Brand;
import com.baizhi.entity.Phone;
import com.baizhi.service.PhoneService;
import com.baizhi.utli.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class PhoneSerciceImpl implements PhoneService {

    //分页查询
    @Override
    public List<Phone> selectPhone(Integer page, Integer rows) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        List<Phone> phones = null;
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            int pageA = (page - 1) * rows + 1;
            int rowsB = page * rows;
            phones = dao.selectPhone(pageA, rowsB);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
        return phones;
    }

    //查询总条数
    @Override
    public Integer selectPhoneSum() {
        SqlSession sqlSession = MyBatisUtils.openSession();
        Integer sum = null;
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            sum = dao.selectPhoneSum();
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
        return sum;
    }


    //增加方法
    @Override
    public void insertPhone(Phone phone) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        Integer sum = null;
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.insertPhone(phone);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    //删除方法
    @Override
    public void deletePhone(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.deletePhone(id);

            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    //查询一条数据
    @Override
    public Phone selectPhoneOne(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        Phone phone = null;
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            phone = dao.selectPhoneOne(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
        return phone;
    }

    //修改数据
    @Override
    public void updatePhone(Phone phone) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.updatePhone(phone);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public List<Brand> selectBrand() {
        SqlSession sqlSession = MyBatisUtils.openSession();
        List<Brand> brands = null;
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            brands = dao.selectBrand();
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
        return brands;
    }

    @Override
    public void insertBrand(Brand brand) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.insertBrand(brand);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void deleteSomePhone(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.deleteSomePhone(id);
            System.out.println("service deletePhone:" + "" + id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void deleteBrand(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            PhoneDAO dao = sqlSession.getMapper(PhoneDAO.class);
            dao.deleteBrand(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }
}
